import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../CIFAR10_dataset", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
dataloader = DataLoader(dataset, batch_size=1)


class Wangqi(nn.Module):
    def __init__(self):
        super(Wangqi, self).__init__()
        self.modle1 = nn.Sequential(
            nn.Conv2d(3, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10)
        )

    def forward(self, x):
        x = self.modle1(x)
        return x


wangqi = Wangqi()
loss = nn.CrossEntropyLoss()
for data in dataloader:
    imgs, targets = data
    output = wangqi(imgs)
    result_loss = loss(output, targets)
    print(result_loss.item())
    result_loss.backward()
    print("OK")